package com.aimashi.railwaytoolmanagesystem.activity.history;

import java.io.File;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ExpandableListView;

import com.aimashi.railwaytoolmanagesystem.R;
import com.aimashi.railwaytoolmanagesystem.bean.GoodMode;
import com.aimashi.railwaytoolmanagesystem.bean.HistoryInfo;
import com.aimashi.railwaytoolmanagesystem.bean.HistorySecondInfo;
import com.aimashi.railwaytoolmanagesystem.bean.RecordMode;
import com.aimashi.railwaytoolmanagesystem.bean.ScanMode;
import com.aimashi.railwaytoolmanagesystem.utils.DatabaseGoodModeUtil;
import com.aimashi.railwaytoolmanagesystem.utils.DatabaseRecordModeUtil;
import com.aimashi.railwaytoolmanagesystem.utils.DatabaseScanModeUtil;
import com.lidroid.xutils.util.LogUtils;

/**
 * 银行卡操作的类
 * 
 * @author zhaoys
 * 
 */
public class Hostory extends Fragment {

	private DatabaseRecordModeUtil dbmRecordModeUtil;
	
	private DatabaseScanModeUtil dbmScanModeUtil;
	
	private DatabaseGoodModeUtil dbmGoodModeUtil;
	

	private View mMainView;
	
	private ExpandableListView recordListView;
	
	// 扫描记录的集合
	List<HistoryInfo> historyInfoList;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		dbmRecordModeUtil = new DatabaseRecordModeUtil(getActivity());
		
		dbmScanModeUtil = new DatabaseScanModeUtil(getActivity());
		
		dbmGoodModeUtil = new DatabaseGoodModeUtil(getActivity());

		LogUtils.i("fragment2-->onCreate()");

		LayoutInflater inflater = getActivity().getLayoutInflater();
		mMainView = inflater.inflate(R.layout.hostory,
				(ViewGroup) getActivity().findViewById(R.id.setting_viewPager),
				false);
		
		File dateDir = getActivity().getExternalFilesDir("/");
		Utility.Path = dateDir.getPath();

		recordListView = (ExpandableListView) mMainView.findViewById(R.id.recordListView);
		
		
		InitModel();
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		LogUtils.i("fragment2-->onCreateView()");

		ViewGroup p = (ViewGroup) mMainView.getParent();
		if (p != null) {
			p.removeAllViewsInLayout();
			LogUtils.i("fragment2-->�Ƴ��Ѵ��ڵ�View");
		}
		return mMainView;
	}

	@Override
	public void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		LogUtils.i("fragment2-->onDestroy()");
	}

	@Override
	public void onPause() {
		// TODO Auto-generated method stub
		super.onPause();
		LogUtils.i("fragment2-->onPause()");
	}

	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		LogUtils.i("fragment2-->onResume()");
	}

	@Override
	public void onStart() {
		// TODO Auto-generated method stub
		super.onStart();
		LogUtils.i("fragment2-->onStart()");
	}

	@Override
	public void onStop() {
		// TODO Auto-generated method stub
		super.onStop();
		LogUtils.i("fragment2-->onStop()");
	}
	
	// 初始化实体类
		public void InitModel() {
			 historyInfoList  = new ArrayList<HistoryInfo>();
			List<RecordMode> recordMode = dbmRecordModeUtil.findAllRecordModeByDate("");
			if(null != recordMode){
				for(RecordMode firstItem:recordMode ){
					HistoryInfo historyInfo = new HistoryInfo();
					historyInfo.setRecordMode(firstItem);
					HistorySecondInfo historySecondInfo = null;
					List<HistorySecondInfo> historySecondInfoList = new ArrayList<HistorySecondInfo>();
					List<ScanMode> scanMode = dbmScanModeUtil.findAllScanModeByEndDateCount(firstItem.getRecordDate());
					for(ScanMode secondItem:scanMode ){
						historySecondInfo = new HistorySecondInfo();
						List<GoodMode> goodMode = dbmGoodModeUtil.findAllGoodModeByEndDateCount(secondItem.getEndDate()+secondItem.getScanCount());
						historySecondInfo.setScanMode(secondItem);
						historySecondInfo.setGoodModeList(goodMode);
						historySecondInfoList.add(historySecondInfo);
					}
					historyInfo.setHistorySecondInfoList(historySecondInfoList);
					historyInfoList.add(historyInfo);
				}
			}
			if (historyInfoList != null)
				recordListView.setAdapter(new RecordAdapter(getActivity(), historyInfoList));
			
			recordListView.setGroupIndicator(null);
			Utility.setExpandableListViewHeight(recordListView);
		}
}
